﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using FetchDataSystem.DAL;
using FetchDataSystem.Model;

namespace FetchDataSystem
{
    public partial class FrmClockInAndOff : Form
    {
        public FrmClockInAndOff()
        {
            InitializeComponent();
        }
        //刷卡时，记录员工上下班刷卡信息
        private void BtnBrush_Click(object sender, EventArgs e)
        {
            //1.创建读取读卡器数据的com端口
            SerialPort com3 = new SerialPort("com3", 9600, Parity.Even, 8, StopBits.One);
            com3.Open();
            string employeeNo = ModBus.ModBusRead(com3);

            //2.根据员工号判断是否是员工
            Employee employee = MountGuardService.GetEmployeeByEmployeeNo(employeeNo);
            if (employee == null)
            {
                MessageBox.Show("员工号错误，请联系管理员！", "提示");
                if (com3.IsOpen)
                {
                    com3.Close();
                }
                return;
            }
            //3.是员工，则根据员工id判断是否绑定工位
            string employeeCellId = MountGuardService.GetEmployeeCellByEmployeeId(employee.id);
            if (employeeCellId == null)
            {
                MessageBox.Show("员工未与工位匹配，请联系管理员!", "提示");
                if (com3.IsOpen)
                {
                    com3.Close();
                }
                return;
            }
            //4.是绑定工位则获得最近一次的打卡记录
            MountGuard mountGuard = MountGuardService.GetMountGuardByEmplloyeeNo(employeeNo);
            string message = "刷卡失败！请联系管理员！";
            if (mountGuard == null)//上班打卡
            {
                mountGuard = new MountGuard();
                mountGuard.id = Guid.NewGuid().ToString();
                mountGuard.EmployeeId = employee.id;
                mountGuard.EmployeeNo = employeeNo;
                mountGuard.EmployeeName = employee.EmployeeName;
                mountGuard.ClockIn = DateTime.Now;
                mountGuard.WorkStatus = "上班";
                mountGuard.DelFlag = "0";
                mountGuard.CreateBy = Session.UserId;//当前用户的id
                mountGuard.CreateDate = DateTime.Now;
                mountGuard.UpdateBy = Session.UserId;
                mountGuard.UpdateDate = DateTime.Now;
                //调用保存方法
                int result = MountGuardService.InsertMountGuard(mountGuard);
                if (result > 0)
                {
                    message = "上岗成功！";
                }
            }
            else//下班打卡
            {
                mountGuard.ClockOff = DateTime.Now;
                mountGuard.WorkStatus = "下班";
                mountGuard.UpdateBy = Session.UserId;
                mountGuard.UpdateDate = DateTime.Now;
                //调用更新方法
                int result = MountGuardService.UpdateMountGuard(mountGuard);
                if (result > 0)
                {
                    message = "下岗成功！";
                }
            }
            MessageBox.Show(message, "提示");
            if (com3.IsOpen)
            {
                com3.Close();
            }
            
            return;
        }
    }
}
